#include<bits/stdc++.h>
using namespace std;
#define int long long
const int M = (1<<17)+5;
int tot =0;
int K;
int n,m;
int aa[M+5];
int a[M+5],c[M+5],cc[M+5];
int res[M+5];
bool ok[18][M+5];
int mp[18][M+5];
int lg[M+5];
int lld[M+5];
int L[(M<<2)+5],R[(M<<2)+5],to[(M<<2)+5];
struct node{
	int o,l,r,d,pos;
	int v;
}tree[(M<<2)+5];
void build(int l,int r,int k,int d){
	tree[k].l = l,tree[k].r = r;
	tree[k].d = d;
	tree[k].o = ok[d][l];
	mp[d][l] = k;
	//if(d ==K)cout << l << " " << r << " " << tree[k].o << " " << l << " " << d << endl;
	if(l+1 == r){
		tree[k].v = a[l];tree[k].pos = l;
	//	cout << a[l] << endl;
		return;	
	}
	build(l,l+r>>1,k<<1,d-1),build(l+r>>1,r,k<<1|1,d-1);
	int now = k,v = tree[k].v;
	if(!tree[k].o){
		if(tree[k<<1].v>=d)tree[k].v = tree[k<<1].v,tree[k].pos = tree[k<<1].pos;
		else tree[k].v = tree[k<<1|1].v,tree[k].pos = tree[k<<1|1].pos;
	}else{
		if(tree[k<<1|1].v>=d)tree[k].v = tree[k<<1|1].v,tree[k].pos = tree[k<<1|1].pos;
		else tree[k].v = tree[k<<1].v,tree[k].pos = tree[k<<1].pos;
	}
	//cout << " " << l << " " << r << " " << tree[k].v << endl;
}
int C[20];
long long ans[M+5];
long long slove(int l){
	//cout << "? " << l << endl;
	int k = 0;
	while((1<<k)<l)++k;
	//if(l == (1<<k))return 1;
	int pos = 1,mx = l;
	vector<pair<int,int> >p;
	int lim = 0;
	for(int i =k;i>=0;i--){
		if(l>>i&1){
			int id = mp[i][pos];
	//		cout << "   " << pos << " " << i << " " << id << " " << R[id] << endl;
			mx = max(mx,R[id]-1);
			p.push_back({id,tree[id].v});
			pos+=1<<i;
		}
	}
	reverse(p.begin(),p.end());
	for(int i =0;i<=K;i++)C[i] = 0;
	C[p[0].second] = tree[p[0].first].pos;
	//cout << tree[p[0].first].pos << endl;
	for(int i = 1;i<p.size();i++){
		int id = p[i].first;
		if(tree[id>>1].o == 0){
			if(tree[id].v > tree[id].d){
				for(int j = 0;j<=K;j++)C[j] = 0;
				C[p[i].second] = tree[id].pos;
			}
		}else{
			int ok = 0;
			for(int i = 0;i<=tree[id].d;i++){
				if(C[i])ok = 1;
				C[i] = 0;
			}
			if(ok)C[p[i].second]+=tree[id].pos;
		}
	}
	int tot =0;
	for(int i = 0;i<=K;i++)tot+=C[i];
	long long ll = mx+1,rr = (1<<k);
	if(ll>rr)ll = rr+1;
	//cout << " " << l << " " << ll << " " << rr << " " << tot << endl;
	long long res = tot+(ll+rr)*(rr-ll+1)/2;
	return res;
}

void solve(){
	int X[4];
	for(int i =0;i<4;i++)cin >> X[i];
	for(int i = 1;i<=n;i++)a[i] = aa[i]^X[i%4];
	for(int i = 1;i<=n;i++)a[i] = min(a[i],K);
//	for(int i = 1;i<=n;i++)cout << " " << a[i];cout <<endl;
	build(1,tot+1,1,K);
	for(int i =1;i<2*tot;i++){
		int now = i;
		while(now>1 and tree[now>>1].o == (now&1) and tree[now].v>=tree[now>>1].d)now>>=1;

		L[i] = tree[now].l,R[i] = tree[now].r;
	}
	//cout << tree[1].pos << endl;
	for(int i = 1;i<=m;i++){
		ans[i] = slove(c[i]);
	}
//	for(int i =1;i<=m;i++)cout << ans[i] << " ";cout << endl;
	long long res = 0;
	for(int i = 1;i<=m;i++)res^=(ans[i]*i);
	cout << res << endl;
	//cout <<endl;
}
signed main(){

	//freopen("/home/csps/arena3.in","r",stdin);
	freopen("arena.in","r",stdin);
	freopen("arena.out","w",stdout);
	ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	cin >> n >> m;
	for(int i = 1;i<=n;i++)cin >> aa[i];
	for(int i = 1;i<=m;i++)cin >> c[i];
	while((1<<K)<n)++K;tot = 1<<K;
	lg[1] = 0;
	for(int i = 2;i<=tot;i++)lg[i]=lg[i>>1]+1;
	for(int i = 1;i<=tot;i++)lld[i]=1<<lg[i];
	for(int i =1;i<=K;i++){
		string s;cin >> s;
		int l =0 ;
		//cout << s.size() << endl;
		for(int j = 1;j<=tot;j+=(1<<i)){
			ok[i][j] = (s[l++]-'0');
			//char c;cin >> c;
			//cout << " " << i << " " << j << " " << C << endl;
			//if(c == '1')ok[i][j]=1;
			//else ok[i][j]=0;
		}
	}
	//cout << ok[9][1] << endl;return 0;
	int t;cin >> t;//t = 1;
	while(t--)solve();
	return 0;
}
/*
g++ arena.cpp -o qwq -std=c++14 -fsanitize=address,undefined
*/